﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Random = UnityEngine.Random;

public class LocalData : Singleton<LocalData>
{
    private string m_name = "Local_PlayerName_";
    private string m_head = "Local_PlayerHead_";
    private string m_GoldCoin = "Local_Player_GoldCoin_";
    
    private string recordwin4 = "Local_PlayerRecord_Win4_";
    private string recordFail4 = "Local_PlayerRecord_Fail4_";

    private string recordwin2 = "Local_PlayerRecord_Win2_";
    private string recordFail2 = "Local_PlayerRecord_Fail2_";

    public string scoring = "Local_PlayerData_Ranking_";

    public string Voice = "Local_Voice_";

    //获取用户头像
    public string OnSetPlayerHead()
    {
        string head = PlayerPrefs.GetString(m_head);
        if (head == null || head == "")
        {
            // 随机一个名字给他
            string playerhead = "touxiang"+Random.Range(1,4);
            PlayerPrefs.SetString(m_head,playerhead);
            return playerhead;
        }

        return head;
    }
    
    //获取用户名
    public string OnSetOrGetPlayerName()
    {
        string name = PlayerPrefs.GetString(m_name);
        if (name == null || name == "")
        {
            // 随机一个名字给他
            name = otherPlayerName[Random.Range(1,otherPlayerName.Count)];
            PlayerPrefs.SetString(m_name,name);
            return name;
        }

        return name;
    }
    //获取玩家身上有多少金币
    public int OnGetPlayerGold()
    {
        int num = PlayerPrefs.GetInt(m_GoldCoin);
        if (num <= 0)
        {
            int goldcoin = 10000;
            PlayerPrefs.SetInt(m_GoldCoin,goldcoin);
            return goldcoin;
        }
        return num;
    }
    public void OnSetPlayerGold(int goldnum)
    {
        PlayerPrefs.SetInt(m_GoldCoin,goldnum);
    }
    
    /// <summary>
    /// 设置本地的排名输赢等信息
    /// </summary>
    /// <param name="key"></param>
    /// <param name="value"></param>
    /// <returns></returns>
    public int OnSetStringValue(string key)
    {
        if (PlayerPrefs.HasKey(key))
        {
            PlayerPrefs.SetInt(key,0);
            return 0;
        }
        else
        {
            return PlayerPrefs.GetInt(key);
        }
    }
    
    /// <summary>
    /// 设置保存数据
    /// </summary>
    /// <param name="type">1,win 2,fail 3,2v2win 4,2v2fail 5,ranking</param>
    /// <param name="data"></param>
    /// <returns></returns>
    public int OnSetRecordData(int type,int data)
    {
        int shuju = 0;
        switch (type)
        {
            case 1: shuju = OnSetStringValue(recordwin4); break;
            case 2: shuju = OnSetStringValue(recordFail4); break;
            case 3: shuju = OnSetStringValue(recordwin2); break;
            case 4: shuju = OnSetStringValue(recordFail2); break;
            case 5: shuju = OnSetStringValue(scoring); break;
            case 6: shuju = OnSetStringValue(scoring); break;
        }

        return shuju;
    }
    
    
    List<string> otherPlayerName = new List<string>()
    {
        "Trelynn", "Yaroslav", "Atlean", "Dhruvin", "Kule", "Gamar"
    };

    //随机获取一个人的名称
    public string OnGetRandomName()
    {
        return otherPlayerName[Random.Range(0, otherPlayerName.Count)];
    }
    
    //进入游戏的类型是什么
    private int entergame = 0;
    public int EnterGame
    {
        get { return entergame; }
        set { entergame = value; }
    }

    private int entergametype = 0;
    public int EnterGameType
    {
        get { return entergametype; }
        set { entergametype = value; }
    }
    
    private List<Sprite> m_shaiziList = new List<Sprite>();
    //获取筛子数据
    public List<Sprite> OnGetShaizi()
    {
        if (m_shaiziList.Count <= 1)
        {
            m_shaiziList.Add(Resources.Load<Sprite>("Image/Sprite/shaizi/1"));
            m_shaiziList.Add(Resources.Load<Sprite>("Image/Sprite/shaizi/2"));
            m_shaiziList.Add(Resources.Load<Sprite>("Image/Sprite/shaizi/3"));
            m_shaiziList.Add(Resources.Load<Sprite>("Image/Sprite/shaizi/4"));
            m_shaiziList.Add(Resources.Load<Sprite>("Image/Sprite/shaizi/5"));
            m_shaiziList.Add(Resources.Load<Sprite>("Image/Sprite/shaizi/6"));
            m_shaiziList.Add(Resources.Load<Sprite>("Image/Sprite/shaizi/go"));
        }
        return m_shaiziList; 
    }
}
